require(ggplot2)
require(scales)

## ITANES
require(memisc)
# Download from http://www.itanes.org/
itanes_2013 <- as.data.frame(as.data.set(spss.system.file("ITA2013_(envers2015_01_19).sav"))) # Not provided
itanes_voted_m5s_2013 <- subset(itanes_2013, d89=='M5S - Five Stars Movement (leader: Grillo)')
df <- subset(itanes_voted_m5s_2013, !(d45 %in% c('Other','Didn\'t vote','Don\'t remember','No answer')))
df$d45 <- as.character(df$d45)

df <- as.data.frame(prop.table(table(df$d45)))

df$Var1 <- factor(df$Var1, levels=c('Sinistra Arcobaleno - Left Rainbow (leader: Bertinotti)',
                                    'IdV - Italy of Values (leader: Di Pietro)',
                                    'PD -  Democratic Party (leader: Veltroni)',
                                    'PS - Socialist Party (leader: Boselli)',
                                    'DC - Union of Christian and Centre Democrats (leader: Casini)',
                                    'PDL - The People of Freedom (leader: Berlusconi)',
                                    'LN - Northern League (leader: Bossi)'))
df$source <- 'Provenance of M5S votes casted in 2013 relatively to 2008 election'

m5s_vote_prov <- df # to use later

right_vote_perc <- sum(m5s_vote_prov$Freq[grepl('^DC -|^LN -|^PDL -',m5s_vote_prov$Var1)])
left_vote_perc <- sum(m5s_vote_prov$Freq[grepl('^PD -|^Sinistra|^PS -|^IdV -',m5s_vote_prov$Var1)])

df <- rbind(df, 
            data.frame(Var1 = c('Sinistra Arcobaleno - Left Rainbow (leader: Bertinotti)',
                                'IdV - Italy of Values (leader: Di Pietro)',
                                'PD -  Democratic Party (leader: Veltroni)',
                                'PS - Socialist Party (leader: Boselli)',
                                'DC - Union of Christian and Centre Democrats (leader: Casini)',
                                'PDL - The People of Freedom (leader: Berlusconi)',
                                'LN - Northern League (leader: Bossi)'),
                       Freq = c(0.0312,0.0437,0.3318,0.098,0.0562,0.3738,0.0830),
                       source = "National result in 2008 election"))

print(ggplot(data=df, aes(x=Var1, y=Freq, fill=source)) + 
  geom_bar(position = 'dodge', stat='identity') +
  geom_text(aes(label=round(Freq*100,digits=2)), position=position_dodge(width=0.9), vjust=-0.25, size=3)  + 
  scale_y_continuous(labels=scales::percent) +
  labs(x=(paste0('(n=',nrow(itanes_voted_m5s_2013),')')), y=NULL) +
  scale_x_discrete(labels=c("SA","IdV","PD","PS","DC","PDL","LN")) +
  guides(fill=guide_legend(title=NULL)) +
  theme(legend.position="bottom"))